package com.sunlei.webtest.springandrabbitmq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.support.ConsumerTagStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.UUID;

/**
 * @Author: sun
 * @Description:
 * @Data: create in 11:19 2018/11/24
 * @Modfied by:
 */

@Configuration
@ComponentScan({"com.sunlei.webtest.*"})
public class RabbitMQConfig {

    @Bean
    public ConnectionFactory connectionFactory(){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses("127.0.0.1:5672");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");
        return connectionFactory;
    }

   @Bean
   public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
   }

   //自定义Exchange,Queue,Binding
   @Bean
    public TopicExchange exchange(){
       return new TopicExchange("beanExchange",false,true);
   }

   @Bean
    public Queue queue1(){
        return new Queue("beanQueue1",true);
   }

    @Bean
    public Queue queue2(){
        return new Queue("beanQueue2",true);
    }

    @Bean
    public Binding binding1(){
        return BindingBuilder.bind(queue1()).to(exchange()).with("bean.#");
   }

    @Bean
    public Binding binding2(){
        return BindingBuilder.bind(queue2()).to(exchange()).with("bean.#");
    }
   @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        return rabbitTemplate;
   }

   @Bean
    public SimpleMessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory){
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setQueues(queue1(),queue2());
        //当前的消费者数量
        container.setConcurrentConsumers(1);
        //最大的消费者数量
       container.setMaxConcurrentConsumers(5);
       //是否使用重回队列
       container.setDefaultRequeueRejected(false);
       //设置签收方式
       container.setAcknowledgeMode(AcknowledgeMode.AUTO);
       //消费端自己的标签策略
       container.setConsumerTagStrategy(new ConsumerTagStrategy() {
           @Override
           public String createConsumerTag(String queue) {
               //Consumer tag
               return queue+"_"+ UUID.randomUUID().toString();
           }
       });
       //接收消息后的监听事件
       /*container.setMessageListener(new ChannelAwareMessageListener() {
           @Override
           public void onMessage(Message message, Channel channel) throws Exception {
                String msg= new String(message.getBody());
               System.err.println("[消费者收到]:"+msg);
           }
       });*/

//       MessageListenerAdapter adapter = new MessageListenerAdapter(new MessageDele)
        return container;
   }
}
